//
//  AnswerListViewController.m
//  RainbowFM
//
//  Created by chenli on 14/12/13.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "AnswerListViewController.h"
#import "UtilsCell.h"
#import "ActivityModel.h"
#import "AnswerDetailViewController.h"
#import "LoginModel.h"

@interface AnswerListViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>{
    NSMutableArray *listArray;
}
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation AnswerListViewController


-(void)fetchListData{
    [listArray removeAllObjects];
    [[ActivityModel sharedModel] fetchPrizeList:^(BOOL success, NSArray *array) {
        [listArray addObjectsFromArray:array];
        [self.collectionView reloadData];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    listArray = [NSMutableArray array];
    [self fetchListData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return listArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    AnswerCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AnswerCollectionCell" forIndexPath:indexPath];
    PrizeListItem *item = listArray[indexPath.row];
    cell.titleLabel.text = item.prizeName;
    cell.priceLabel.text = item.prizeDesc;
    [cell.cellImgeView sd_setImageWithURL:[NSURL URLWithString:item.prizeUrl]];
    
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([LoginModel sharedModel].userID != nil) {
        PrizeListItem *item = listArray[indexPath.row];
        if ([item.actStatus isEqualToString:@"3"]) {
            [SVProgressHUD showErrorWithStatus:@"答题活动已结束"];
            return;
        }
        
        if ([item.actStatus isEqualToString:@"1"]) {
            [SVProgressHUD showErrorWithStatus:@"答题活动还未开始"];
            return;
        }
        
        if ([item.qstFlag isEqualToString:@"1"]) {
            [SVProgressHUD showErrorWithStatus:@"您已答题，请选择其他目录"];
            return;
        }
        
        AnswerDetailViewController *anserDetailView = [self.storyboard instantiateViewControllerWithIdentifier:@"AnswerDetailViewController"];
        anserDetailView.actId = item.actId;
        [self.navigationController pushViewController:anserDetailView animated:YES];
    }else{
        UINavigationController *controller = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateInitialViewController];
        [self presentViewController:controller animated:YES completion:nil];
    }
   
}


#pragma mark - Navigation




@end
